package com.dongdongshop.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbUserScoreUseMapper;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.TbUserScoreDetail;
import com.dongdongshop.pojo.TbUserScoreUse;
import com.dongdongshop.service.UserScoreAccountService;
import com.dongdongshop.service.UserScoreDetailService;
import com.dongdongshop.service.UserScoreUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @ClassName UserScoreUseServiceImpl
 * @Author RC
 * @date 2020.12.27 17:52
 */
@Component
@Service
public class UserScoreUseServiceImpl implements UserScoreUseService {
    @Autowired
    private TbUserScoreUseMapper tbUserScoreUseMapper;
    @Reference(timeout = 100000,check = false)
    private UserScoreAccountService userScoreAccountService;
    @Reference(timeout = 100000,check = false)
    private UserScoreDetailService userScoreDetailService;

    @Override
    public void insertInfo(String userId, int total_amount) {
        TbUserScoreUse userScoreUse = new TbUserScoreUse();
        userScoreUse.setUserId(userId);
        userScoreUse.setPoint(total_amount);
        DateTime date = DateUtil.date(new Date());
        userScoreUse.setCreateTime(date);
        DateTime date2 = DateUtil.nextMonth();
        userScoreUse.setExpirTime(date2);
        tbUserScoreUseMapper.insertSelective(userScoreUse);
    }
    //兑换积分
    @Transactional
    @Override
    public String Points_exchange(Integer score, String username) {
        List preparedDel = new ArrayList();
        /*根据用户名查userscoreuse积分可用表*/
        List<TbUserScoreUse> userList = tbUserScoreUseMapper.selectUseScore(username);
        /*查出当前用户的总积分情况,方便修改用户总积分*/
        TbUserScoreAccount userScore = userScoreAccountService.findInfoByUserId(username);
        Integer total = 0;
        Integer flag = 0;
        for(int i = 0 ; i < userList.size(); i++){
            total +=userList.get(i).getPoint(); //可用积分的累加
            preparedDel.add(userList.get(i).getId());
            if(total == score){//当前可用积分正好够，那么1.直接删除对应的可用积分数据,2.减掉用户的积分，3.添加积分流水情况
                addScoreDeatail(score,username,userScore,preparedDel);
                return "100"; // 购买成功
            }else if(total > score){//当前可用积分超过需要消耗额积分
                //1判断刚好大于这个score对应的下标位置 删除这个下标前的位置(包括此下标)，1剩余积分在增加到表中即可2.减掉用户的积分，3.添加积分流水情况
                //1.1获取当前坐标下的可用积分
                TbUserScoreUse tbUserScoreUse = tbUserScoreUseMapper.selectByPrimaryKey(userList.get(i).getId());
                Integer rescore = total -score;
                TbUserScoreUse tb2 = new TbUserScoreUse();
                tb2.setPoint(rescore);
                tb2.setExpirTime(tbUserScoreUse.getCreateTime());
                tb2.setCreateTime(new Date());
                tb2.setUserId(tbUserScoreUse.getUserId());
                tbUserScoreUseMapper.insertSelective(tb2);
                //2.扣除用户消耗的积分
                //3添加积分流水情况
                addScoreDeatail(score,username,userScore,preparedDel);
                return "100"; // 购买成功
            }
        }
        if(total < score){ //可用积分不够
            return "200";//200代表积分不够
        }
        return "100";//购买成功
    }
    //3添加积分流水情况方法
    public void addScoreDeatail(Integer score, String username, TbUserScoreAccount userScore, List preparedDel){
        //1.直接删除对应的可用积分数据
        tbUserScoreUseMapper.deleteScore(preparedDel);
        //2.扣除用户消耗的积分
        userScore.setTotalScore(userScore.getTotalScore()-score);
        userScore.setExpendScore(userScore.getExpendScore()+score);//设置消耗积分
        //3添加积分流水情况
        userScoreAccountService.update(userScore);
        TbUserScoreDetail tb = new TbUserScoreDetail();
        tb.setPoint(-score);
        tb.setCreateTime(new Date());
        tb.setTextarea("使用积分");
        tb.setUserId(username);
        tb.setCreateTime(new Date());
        userScoreDetailService.add(tb);
    }

}

